package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Grade;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.pageinfo.PageRequest;
import com.woniuxy.entity.pageinfo.TableResult;
import com.woniuxy.service.GradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Grade)表控制层
 *成绩表对应接口
 * @author zjx
 * @since 2021-05-14 19:42:02
 */
@Api(value = "成绩表接口",description = "用于实现对程序表的CRUD")
@RestController
@RequestMapping("grade")
public class GradeController {
    /**
     * 服务对象
     */
    @Resource
    private GradeService gradeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条成绩数据")
    @GetMapping("selectOne")
    public ResponseEntity selectOne(@ApiParam("主键ID") Integer id) {
        Grade selectGrade = this.gradeService.queryById(id);
        return new ResponseEntity(200,"查询成绩成功",selectGrade);
    }
    /**
     * 传入实体 新增数据
     *
     * @param grade 实例对象
     * @return 影响行数
     */
    @ApiOperation("新增成绩表信息")
    @PostMapping("insertOne")
    public ResponseEntity insert(@ApiParam("成绩表实例对象") @RequestBody Grade grade){
        Grade insertGrade = this.gradeService.insert(grade);
        return new ResponseEntity(200,"新增成绩信息成功",insertGrade);
    }

    /**
     * 修改数据
     *
     * @param grade 实例对象
     * @return 影响行数
     */
    @ApiOperation("修改成绩表信息")
    @PostMapping("updateOne")
    public ResponseEntity update(@ApiParam("成绩表实例对象") @RequestBody Grade grade){
        Grade updateGrade = this.gradeService.update(grade);
        return new ResponseEntity(200,"修改成绩信息成功",updateGrade);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键删除单条成绩数据")
    @GetMapping("deleteOne")
    public ResponseEntity delete(@ApiParam("主键ID") int id){
        boolean deleteGrade = this.gradeService.deleteById(id);
        if (this.gradeService.deleteById(id )){
            return new ResponseEntity(200,"删除信息成功",deleteGrade);
        }else{
            return new ResponseEntity(500,"删除信息失败",null);
        }

    }

    @ApiOperation("阅卷管理首页")
    @GetMapping(value = "gradeExamPaperIndex", produces = {"application/json;charset=utf-8"})
    public TableResult<Grade> gradeExamPaperIndex(@ApiParam("分页请求实体类") PageRequest pageRequest,
    @ApiParam("分数实体类-需要status,grade.exam.examName,grade.exam.examPaper.utility,grade.exam.is_make_up_exam")
            Grade grade) {
        PageInfo<Grade> gradePageInfo  =  gradeService.gradeExamPaperIndex(grade,pageRequest);
        return new TableResult<Grade>(gradePageInfo.getTotal(),gradePageInfo.getList());
    }
}
